from tkinter import * from tkinter import messagebox from Crypto.Cipher import AES import base64 import pymysql class Application(Frame): dbconn=False login=False fullname=None def __init__(self,master): Frame.__init__(self,master) #menu code start self.menubar = Menu(self) self.opmenu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label="Options", menu=self.opmenu) self.opmenu.add_command(label="Home",command=self.strtpage) self.opmenu.add_command(label="Insert",command=self.insertpg) self.opmenu.add_command(label="View",command=self.viewpg) semenu=Menu(self.menubar, tearoff=0) self.opmenu.add_cascade(label="Search",menu=semenu) semenu.add_command(label="By Roll No", command=self.searchpg) semenu.add_command(label="By Name", command=self.schnmpg) self.opmenu.add_command(label="Update",command=self.updatepg) self.opmenu.add_command(label="Delete",command=self.delpg) self.opmenu.add_separator() self.opmenu.add_command(label="Exit",command=self.exitbtn) abmenu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label="Help", menu=abmenu) abmenu.add_command(label="About",command=self.newwin) self.master.config(menu=self.menubar) #menu code end self.currentframe=None self.dbconn=False self.strtpage() def newwin(self): win1 = Toplevel(self) w=400 h=150 ws = self.winfo_screenwidth() # width of the screen hs = self.winfo_screenheight() # height of the screen x = (ws/2) - (w/2) y = (hs/2) - (h/2) win1.geometry('%dx%d+%d+%d' % (w, h, x, y)) win1.resizable(width=False, height=False) win1.title("About") win1.iconbitmap("favicon.ico") def quit_win1(): win1.destroy() lb=Label(win1, text="This program insert, view and delete\ndata from the student database.\nDesigned by Amit.\n\u00a9 2017",justify=LEFT) lb.config(font=("Courier", 12)) lb.pack() btn=Button(win1, text='OK', command=quit_win1, font=("Courier",16,"bold")) btn.pack(pady=10,ipadx=20) win1.grab_set() def exitbtn(self): result = messagebox.askquestion("Exit", "Are You Sure to Exit?", icon='warning') if result == 'yes': root.destroy() def loginbtn(self): user=self.luser.get() passw=self.lpass.get() if(not user or not passw): messagebox.showerror("Error","Username or Password field is empty") else: try: self.cur.execute("SELECT * FROM users WHERE name=%s",user) if(self.cur.rowcount==1): key = b'1234567890123456' d=self.cur.fetchone() nonce=base64.b64decode(bytes(d[4],'utf-8')) epass=base64.b64decode(bytes(d[3],'utf-8')) cipher = AES.new(key, AES.MODE_EAX, nonce) passv = cipher.decrypt(epass) try: passv=passv.decode('utf-8') if(passv==passw): self.login=True self.fullname=d[2] self.strtpage() else: messagebox.showerror("Error","Invalid Password") except: messagebox.showerror("Error","Invalid Password") else: messagebox.showerror("Error","Invalid Username") except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Login Failed") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","Login Failed.\nError: "+str(e)) def strtpage(self,msg=None): self.luser=StringVar() self.lpass=StringVar() if(self.currentframe): self.currentframe.destroy() self.strt=Frame() label2=Label(self.strt, text="Student Database",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack(pady=20) if(self.dbconn==False): mlb=Label(self.strt,text = "Not Connected.Press button to connect") mlb.config(font=("Courier", 16,"bold")) mlb.pack() btn=Button(self.strt,text="Connect",command=self.conbtn) btn.config(font=("Courier", 14,"bold italic")) btn.pack(pady=10,ipadx=5,ipady=5) if(msg): mlb2=Label(self.strt,text = msg) mlb2.config(font=("Courier", 14,"bold italic")) mlb2.pack(pady=30) btn.bind('',lambda x:self.conbtn()) btn.focus() if(self.login): login=Frame(self.strt,borderwidth=2,height=50,width=40,relief=RIDGE) login.pack(pady=20,ipadx=10,ipady=10) lbin=Label(login,text="Logged in as "+self.fullname,font=("Courier", 14,"bold"),fg="blue") lbin.grid(row=0,column=0) lbtn=Button(login,text="Log Out",font=("Courier", 12,"bold"),command=self.logoutbtn) lbtn.bind('',lambda x:self.logoutbtn()) lbtn.grid(row=1,column=0,columnspan=2,ipadx=5,ipady=5,padx=5,pady=5) self.opmenu.entryconfig(1,state="disabled") self.opmenu.entryconfig(2,state="disabled") self.opmenu.entryconfig(3,state="disabled") self.opmenu.entryconfig(4,state="disabled") self.opmenu.entryconfig(5,state="disabled") else: mlb=Label(self.strt,text = "Connection Established with Database") mlb.config(font=("Courier", 16,"bold")) mlb.pack(pady=10) mlb1=Label(self.strt,text = "Go to Options for more.") mlb1.config(font=("Courier", 14,"italic")) mlb1.pack(pady=10) self.opmenu.entryconfig(1,state="disabled") self.opmenu.entryconfig(2,state="disabled") self.opmenu.entryconfig(3,state="disabled") self.opmenu.entryconfig(4,state="disabled") self.opmenu.entryconfig(5,state="disabled") if(not self.login): login=Frame(self.strt,borderwidth=2,height=50,width=40,relief=RIDGE) login.pack(pady=20,ipadx=10,ipady=10) lbin=Label(login,text="You need to Log-In",font=("Courier", 14,"bold"),fg="red") lbin.grid(row=0,column=0,columnspan=2) llb1=Label(login,text="Username",font=("Courier", 12)) llb1.focus() llb1.grid(row=1,column=0,sticky=W,pady=5,padx=3) luser=Entry(login,width=30,textvariable=self.luser,font=("Courier", 12)) luser.grid(row=1,column=1) llb1=Label(login,text="Password",font=("Courier", 12)) llb1.grid(row=2,column=0,sticky=W,pady=5,padx=3) lpass=Entry(login,show="*",textvariable=self.lpass,width=30,font=("Courier", 12)) lpass.bind('',lambda x:self.loginbtn()) lpass.grid(row=2,column=1) lbtn=Button(login,text="Log-In",font=("Courier", 12,"bold"),command=self.loginbtn) lbtn.grid(row=3,column=0,columnspan=2,ipadx=5,ipady=5,padx=5,pady=5) lbtn.bind('',lambda x:self.loginbtn()) else: login=Frame(self.strt,borderwidth=2,height=50,width=40,relief=RIDGE) login.pack(pady=20,ipadx=10,ipady=10) lbin=Label(login,text="Logged in as "+self.fullname,font=("Courier", 14,"bold"),fg="blue") lbin.grid(row=0,column=0) lbtn=Button(login,text="Log Out",font=("Courier", 12,"bold"),command=self.logoutbtn) lbtn.bind('',lambda x:self.logoutbtn()) lbtn.grid(row=1,column=0,columnspan=2,ipadx=5,ipady=5,padx=5,pady=5) self.opmenu.entryconfig(1,state="normal") self.opmenu.entryconfig(2,state="normal") self.opmenu.entryconfig(3,state="normal") self.opmenu.entryconfig(4,state="normal") self.opmenu.entryconfig(5,state="normal") self.strt.pack(fill=BOTH, expand=YES) self.currentframe=self.strt def logoutbtn(self): self.login=False self.strtpage() def conbtn(self): self.dbconnect() if(not self.dbconn): self.strtpage("Connection Not Established") else: self.strtpage() def dbconnect(self): try: self.conn = pymysql.connect(host='localhost', user='root', passwd='', db='python') self.cur = self.conn.cursor() self.dbconn= True except Exception as e: self.dbconn= False def insertpg(self): self.fname=StringVar() self.rbvalue=StringVar() self.currentframe.destroy() self.insert=Frame() f1 = Frame(self.insert) f1.pack() f3 = Frame(self.insert, height = 30) f3.pack(side="bottom", pady = 10) label2=Label(f1, text="Insert Data",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack() f2 = Frame(f1,pady=10,relief=SUNKEN) f2.pack() label1=Label(f2,text="Name", width=10,anchor=E) label1.grid(column=0,row=0,sticky=W,pady=5,padx=10) name=Entry(f2,width=54,textvariable=self.fname) name.grid(row=0,column=1,pady=5,columnspan=3) label3=Label(f2,text="Address", width=10,anchor=E) label3.grid(column=0,row=1,pady=5,sticky=W+N,padx=10) self.addr=Text(f2, height=8,width=40) self.addr.grid(row=1,column=1, columnspan=3,pady=5) scrollb =Scrollbar(f2, command=self.addr.yview,width=15) scrollb.grid(row=1, column=4, sticky="nsw") self.addr['yscrollcommand'] = scrollb.set label4=Label(f2,text="Gender", width=10,anchor=E) label4.grid(column=0,row=2,pady=5,sticky=W+N,padx=10) rb1=Radiobutton(f2,text="Male",variable=self.rbvalue, value="Male") rb2=Radiobutton(f2,text="Female",variable=self.rbvalue, value="Female") rb3=Radiobutton(f2,text="Other",variable=self.rbvalue, value="Other") rb1.select() rb1.grid(row=2,column=1,sticky=W) rb2.grid(row=2,column=2,sticky=W) rb3.grid(row=2,column=3,sticky=W) button1= Button(f3, text="Insert", width=15,command=self.insertdata) button1.pack(side=RIGHT) self.insert.pack(fill=BOTH, expand=YES) self.currentframe=self.insert def insertdata(self): name=self.fname.get() address=self.addr.get('1.0','end').strip() gender=self.rbvalue.get() if(not name or not address): messagebox.showwarning("Error","Name or Address field is empty.\nData not Submitted.") elif(len(name)>50): messagebox.showwarning("Error","Name should be within 50 characters") else: try: sql="INSERT INTO student(name,address,gender)VALUES('"+name+"','"+address+"','"+gender+"')" self.cur.execute(sql) r=self.cur.lastrowid messagebox.showinfo("Info","Registration Successful. Your roll number is "+str(r)) self.conn.commit() self.insertpg() except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.\nData NOT Inserted.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","Data NOT Inserted.\nError: "+str(e)) def searchpg(self,rollp=None): self.rolls=StringVar() self.currentframe.destroy() self.search=Frame() f1 = Frame(self.search) f1.pack() f4=Frame(self.search) f4.pack() f3 = Frame(self.search, height = 30) f3.pack(side="bottom", pady = 10) label2=Label(f1, text="Search by Roll No",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack() f2 = Frame(self.search,pady=10) f2.pack(pady=10) label1=Label(f1,text="Enter Roll No", width=15) label1.pack(side=LEFT,pady=20) name1=Entry(f1,width=10,textvariable=self.rolls) name1.bind('',lambda x:self.schbtn('s')) name1.focus() name1.pack(side=LEFT) self.search.pack(fill=BOTH, expand=YES) self.currentframe=self.search if(rollp): label4=Label(f4,text="Search Results", width=15) label4.config(font=("Courier", 16,"bold")) label4.pack(anchor=W,side=BOTTOM,fill=X) rollp=str(rollp) sql="SELECT * FROM student WHERE id='"+rollp+"'" try: self.cur.execute(sql) d=self.cur.fetchone() #print('Roll: ',d[0],', Name: ',d[1],', Address: ',d[2],', Gender: ',d[3]) lb1=Label(f2,text="Roll No", width=10,anchor=W) lb1.grid(row=0,column=0,sticky=N) lb2=Label(f2,text=d[0], width=50,relief=SUNKEN,anchor=W) lb2.grid(row=0,column=1,pady=5) lb3=Label(f2,text="Name", width=10,anchor=W) lb3.grid(row=1,column=0,sticky=N) lb4=Label(f2,text=d[1], width=50,relief=SUNKEN,anchor=W) lb4.grid(row=1,column=1,pady=5) lb5=Label(f2,text="Address", width=10,anchor=W) lb5.grid(row=2,column=0,sticky=N) lb6=Text(f2, width=44,relief=SUNKEN,height=5,bg=root.cget('bg')) lb6.insert("1.0",d[2]) lb6.configure(state="disabled") scrollb =Scrollbar(f2, command=lb6.yview,width=15) scrollb.grid(row=2, column=2, sticky="nsw") lb6['yscrollcommand'] = scrollb.set lb6.grid(row=2,column=1,pady=5) lb7=Label(f2,text="Gender", width=10,anchor=W) lb7.grid(row=3,column=0,sticky=N) lb8=Label(f2,text=d[3], width=50,relief=SUNKEN,anchor=W) lb8.grid(row=3,column=1,pady=5) except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) button1= Button(f3, text="Search", width=15,command=lambda:self.schbtn('s')) button1.pack(side=RIGHT) def schbtn(self,c): roll=self.rolls.get() if(not roll): messagebox.showwarning("Error","Enter roll number.") else: try: sql="SELECT * FROM student WHERE id='"+roll+"'" self.cur.execute(sql) if(self.cur.rowcount is 1): if(c == 's'): self.searchpg(roll) elif(c=='u'): self.updatepg(roll) else: self.delpg(roll) else: messagebox.showerror("Error","Roll Number doesnt exist.") except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) def delpg(self,rollp=None): self.rolls=StringVar() self.currentframe.destroy() self.search=Frame() f1 = Frame(self.search) f1.pack() f4=Frame(self.search) f4.pack(fill=X) f3 = Frame(self.search, height = 30) f3.pack(side="bottom", pady = 10) label2=Label(f1, text="Delete Entry",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack() f2 = Frame(self.search,pady=10) f2.pack(pady=10) label1=Label(f1,text="Enter Roll No", width=15) label1.pack(side=LEFT,pady=20) name1=Entry(f1,width=10,textvariable=self.rolls) name1.bind('',lambda x:self.schbtn('d')) name1.focus() name1.pack(side=LEFT) self.search.pack(fill=BOTH, expand=YES) self.currentframe=self.search if(rollp): self.droll=rollp label4=Label(f4,text="Are you sure you want to delete this entry?", width=14) label4.config(font=("Courier", 16,"bold")) label4.pack(anchor=W,side=BOTTOM,fill=X) rollp=str(rollp) sql="SELECT * FROM student WHERE id='"+rollp+"'" try: self.cur.execute(sql) d=self.cur.fetchone() #print('Roll: ',d[0],', Name: ',d[1],', Address: ',d[2],', Gender: ',d[3]) lb1=Label(f2,text="Roll No", width=10,anchor=W) lb1.grid(row=0,column=0,sticky=N) lb2=Label(f2,text=d[0], width=50,relief=SUNKEN,anchor=W) lb2.grid(row=0,column=1,pady=5) lb3=Label(f2,text="Name", width=10,anchor=W) lb3.grid(row=1,column=0,sticky=N) lb4=Label(f2,text=d[1], width=50,relief=SUNKEN,anchor=W) lb4.grid(row=1,column=1,pady=5) lb5=Label(f2,text="Address", width=10,anchor=W) lb5.grid(row=2,column=0,sticky=N) lb6=Text(f2, width=44,relief=SUNKEN,height=5,bg=root.cget('bg')) lb6.insert("1.0",d[2]) lb6.configure(state="disabled") scrollb =Scrollbar(f2, command=lb6.yview,width=15) scrollb.grid(row=2, column=2, sticky="nsw") lb6['yscrollcommand'] = scrollb.set lb6.grid(row=2,column=1,pady=5) lb7=Label(f2,text="Gender", width=10,anchor=W) lb7.grid(row=3,column=0,sticky=N) lb8=Label(f2,text=d[3], width=50,relief=SUNKEN,anchor=W) lb8.grid(row=3,column=1,pady=5) delb=Button(f2,text="Yes, DELETE It",width=50,fg="red",font="16,bold",command=self.delop) delb.grid(row=4,column=0,columnspan=2,pady=10) except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) button1= Button(f3, text="Search Roll", width=15,command=lambda:self.schbtn('d')) button1.pack(side=RIGHT) def delop(self): roll=str(self.droll) sql="DELETE FROM student WHERE id='"+roll+"'" try: if(self.cur.execute(sql)): self.conn.commit() messagebox.showinfo("Info","Data Deleted.\nDeleted Roll: "+str(roll)) self.delpg() else: messagebox.showerror("Error Occured","Delete Failed ") except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) def viewpg(self): self.currentframe.destroy() self.view=Frame() self.view.pack(fill=BOTH,expand=YES) self.currentframe=self.view f1=Frame(self.view) f1.pack() label2=Label(f1, text="View All",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack() f3=Frame(self.view) f3.pack() def on_configure(event): canvas.configure(scrollregion=canvas.bbox('all')) canvas=Canvas(f3,height=350,width=732) canvas.grid(row=1,column=0,columnspan=4) scrollbar =Scrollbar(f3, command=canvas.yview,width=15) scrollbar.grid(row=1, column=4, sticky="nsw") canvas.configure(yscrollcommand=scrollbar.set) canvas.bind('',on_configure) f2 = Frame(canvas) canvas.create_window((0,0), window=f2, anchor='nw') try: sql="SELECT * FROM student" self.cur.execute(sql) d=self.cur.fetchall() n=self.cur.rowcount if(n==0): messagebox.showinfo("Info","No Data Present") self.strtpage() else: rollh=Label(f3,text="ROLL NO",width=8,borderwidth=2,font=("Ariel 14 bold")) rollh.grid(row=0,column=0,sticky="ew") nameh=Label(f3,text="NAME",width=15,borderwidth=2,font="Ariel 14 bold") nameh.grid(row=0,column=1,sticky="ew") addrh=Label(f3,text="ADDRESS",width=18,borderwidth=2,font="Ariel 14 bold") addrh.grid(row=0,column=2,sticky="ew") genh=Label(f3,text="GENDER",width=10,borderwidth=2,font="Ariel 14 bold") genh.grid(row=0,column=3,sticky="ew") r=1 for i in range(n): roll=Label(f2,text=d[i][0],width=15,borderwidth=1,relief=SOLID) roll.grid(row=r,column=0,ipady=3,sticky="nsew") name=Text(f2,width=20,height=3,borderwidth=1,relief=SOLID,bg=root.cget('bg'),font=("Courier",14)) name.insert("1.0",d[i][1]) name.configure(state="disabled") name.grid(row=r,column=1,ipadx=5,ipady=3,sticky="nsew") addr=Text(f2,width=30,height=3,borderwidth=1,relief=SOLID,bg=root.cget('bg')) scrollb =Scrollbar(f2, command=addr.yview,width=15) scrollb.grid(row=r, column=3, sticky="nsw") addr['yscrollcommand'] = scrollb.set addr.insert("1.0",d[i][2]) addr.configure(state="disabled") addr.grid(row=r,column=2,ipady=3,sticky="nsew") gen=Label(f2,text=d[i][3],width=17,borderwidth=1,relief=SOLID) gen.grid(row=r,column=4,ipady=3,ipadx=5,sticky="nsew") r=r+1 except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) def schnmop(self): name=self.sname.get() if(not name): messagebox.showwarning("Error","Enter name to be search.") else: try: sql="SELECT * FROM student WHERE name LIKE '%"+name+"%'" print(sql) self.cur.execute(sql) if(self.cur.rowcount > 0): self.schnmpg(name) else: messagebox.showinfo("Invalid","Entered Name doesnt exist on database.") except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) def schnmpg(self,name=None): self.sname=StringVar() self.currentframe.destroy() self.schnm=Frame() self.schnm.pack(fill=BOTH,expand=YES) self.currentframe=self.schnm f1=Frame(self.schnm) f1.pack() label2=Label(f1, text="Search By Name",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack() label1=Label(f1,text="Enter Name", width=15) label1.pack(side=LEFT,pady=20) name1=Entry(f1,width=30,textvariable=self.sname) name1.bind('',lambda x:self.schnmop()) name1.focus() name1.pack(side=LEFT) button1= Button(f1, text="Search Name", width=15,command=self.schnmop) button1.pack(side=LEFT,padx=10) if(name): f3=Frame(self.schnm) f3.pack() def on_configure(event): canvas.configure(scrollregion=canvas.bbox('all')) canvas=Canvas(f3,height=300,width=732) canvas.grid(row=1,column=0,columnspan=4) scrollbar =Scrollbar(f3, command=canvas.yview,width=15) scrollbar.grid(row=1, column=4, sticky="nsw") canvas.configure(yscrollcommand=scrollbar.set) canvas.bind('',on_configure) f2 = Frame(canvas) canvas.create_window((0,0), window=f2, anchor='nw') try: sql="SELECT * FROM student WHERE name LIKE '%"+name+"%'" self.cur.execute(sql) d=self.cur.fetchall() n=self.cur.rowcount if(n==0): messagebox.showinfo("Info","No Data Present") self.strtpage() else: rollh=Label(f3,text="ROLL NO",width=8,borderwidth=2,font=("Ariel 14 bold")) rollh.grid(row=0,column=0,sticky="ew") nameh=Label(f3,text="NAME",width=15,borderwidth=2,font="Ariel 14 bold") nameh.grid(row=0,column=1,sticky="ew") addrh=Label(f3,text="ADDRESS",width=18,borderwidth=2,font="Ariel 14 bold") addrh.grid(row=0,column=2,sticky="ew") genh=Label(f3,text="GENDER",width=10,borderwidth=2,font="Ariel 14 bold") genh.grid(row=0,column=3,sticky="ew") r=1 for i in range(n): roll=Label(f2,text=d[i][0],width=15,borderwidth=1,relief=SOLID) roll.grid(row=r,column=0,ipady=3,sticky="nsew") name=Text(f2,width=20,height=3,borderwidth=1,relief=SOLID,bg=root.cget('bg'),font=("Courier",14)) name.insert("1.0",d[i][1]) name.configure(state="disabled") name.grid(row=r,column=1,ipadx=5,ipady=3,sticky="nsew") addr=Text(f2,width=30,height=3,borderwidth=1,relief=SOLID,bg=root.cget('bg')) scrollb =Scrollbar(f2, command=addr.yview,width=15) scrollb.grid(row=r, column=3, sticky="nsw") addr['yscrollcommand'] = scrollb.set addr.insert("1.0",d[i][2]) addr.configure(state="disabled") addr.grid(row=r,column=2,ipady=3,sticky="nsew") gen=Label(f2,text=d[i][3],width=17,borderwidth=1,relief=SOLID) gen.grid(row=r,column=4,ipady=3,ipadx=5,sticky="nsew") r=r+1 except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) def updatepg(self,rollp=None): self.rolls=StringVar() self.uname=StringVar() self.ugender=StringVar() self.currentframe.destroy() self.update=Frame() f1 = Frame(self.update) f1.pack() f4=Frame(self.update) f4.pack(fill=X) f3 = Frame(self.update, height = 30) f3.pack(side="bottom", pady = 10) label2=Label(f1, text="Update Entry",fg="blue",pady=10) label2.config(font=("Courier", 20,"bold")) label2.pack() f2 = Frame(self.update,pady=10) f2.pack(pady=10) label1=Label(f1,text="Enter Roll No", width=15) label1.pack(side=LEFT,pady=20) name1=Entry(f1,width=10,textvariable=self.rolls) name1.bind('',lambda x:self.schbtn('u')) name1.focus() name1.pack(side=LEFT) self.update.pack(fill=BOTH, expand=YES) self.currentframe=self.update if(rollp): self.uroll=rollp label4=Label(f4,text="Edit this entry", width=14) label4.config(font=("Courier", 16,"bold")) label4.pack(anchor=W,side=BOTTOM,fill=X) rollp=str(rollp) sql="SELECT * FROM student WHERE id='"+rollp+"'" try: self.cur.execute(sql) d=self.cur.fetchone() #print('Roll: ',d[0],', Name: ',d[1],', Address: ',d[2],', Gender: ',d[3]) lb1=Label(f2,text="Roll No", width=10,anchor=W) lb1.grid(row=0,column=0,sticky=N) lb2=Label(f2,text=d[0], width=40,relief=SUNKEN,anchor=W) lb2.grid(row=0,column=1,columnspan=3,pady=5,sticky="ew") lb3=Label(f2,text="Name", width=10,anchor=W) lb3.grid(row=1,column=0,sticky=N) lb4=Entry(f2,textvariable=self.uname,width=50,relief=SUNKEN) lb4.insert(0,d[1]) lb4.grid(row=1,column=1,columnspan=3,pady=5) lb5=Label(f2,text="Address", width=10,anchor=W) lb5.grid(row=2,column=0,sticky=N) self.uaddr=Text(f2, width=38,relief=SUNKEN,height=5) self.uaddr.insert("1.0",d[2]) scrollb =Scrollbar(f2, command=self.uaddr.yview,width=15) scrollb.grid(row=2, column=4, sticky="nsw") self.uaddr['yscrollcommand'] = scrollb.set self.uaddr.grid(row=2,column=1,columnspan=3,pady=5) lb7=Label(f2,text="Gender", width=10,anchor=W) lb7.grid(row=3,column=0,sticky=N) rb1=Radiobutton(f2,text="Male",variable=self.ugender, value="Male") rb2=Radiobutton(f2,text="Female",variable=self.ugender, value="Female") rb3=Radiobutton(f2,text="Other",variable=self.ugender, value="Other") rb1.grid(row=3,column=1,sticky=W) rb2.grid(row=3,column=2,sticky=W) rb3.grid(row=3,column=3,sticky=W) gen=d[3] if(gen=='Male'): rb1.select() elif(gen=='Female'): rb2.select() else: rb3.select() delb=Button(f2,text="UPDATE",width=30,fg="blue",font="16,bold",command=self.updatebtn) delb.grid(row=4,column=0,columnspan=5,pady=10) except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","\nError: "+str(e)) button1= Button(f3, text="Search Roll", width=15,command=lambda:self.schbtn('u')) button1.pack(side=RIGHT) def updatebtn(self): roll=self.uroll name=self.uname.get() addr=self.uaddr.get('1.0','end').strip() gender=self.ugender.get() if(not name or not addr): messagebox.showwarning("Error","Name or Address not Entered.\nData not Updated") elif(len(name)>50): messagebox.showwarning("Error","Name should be within 50 characters.\nData not Updated.") else: try: sql="UPDATE student SET name = %s, address = %s, gender= %s WHERE id= %s" self.cur.execute(sql,(name,addr,gender,roll)) self.conn.commit() messagebox.showinfo("Success","Data Updated Successfully") self.updatepg(self.uroll) except pymysql.err.OperationalError: messagebox.showerror("Connection Error","Connection to Server Lost.\nData NOT Updated.") self.dbconn=False self.strtpage("Connection to Server Lost") except Exception as e: messagebox.showerror("Error Occured","Data NOT Updated.\nError: "+str(e)) root=Tk() root.title("Database Admin") w=820 h=500 ws = root.winfo_screenwidth() # width of the screen hs = root.winfo_screenheight() # height of the screen x = (ws/2) - (w/2) y = (hs/2) - (h/2) root.geometry('%dx%d+%d+%d' % (w, h, x, y)) root.iconbitmap("favicon.ico") app=Application(root) root.protocol("WM_DELETE_WINDOW", app.exitbtn) root.resizable(width=False, height=False) root.mainloop()